package org.jiuling.network.tools.request.http.factory;

import org.jiuling.local.tools.verify.StringVerify;
import org.jiuling.network.tools.request.http.HttpRequest;
import org.jiuling.network.tools.request.http.exception.HttpRequestException;
import org.jiuling.network.tools.request.http.impl.HttpRequestImpl;
import org.jiuling.network.tools.request.http.impl.HttpsRequestImpl;

import java.util.regex.Pattern;

/**
 * Http请求工厂，用于生产Http请求和Https请求
 * @author 紫灵
 * @date 2024年02月20日 00时56分54秒
 */
public class HttpRequestFactory {

    /**
     * 生成Http请求
     * @param path 路径
     * @return Http请求
     * @throws HttpRequestException Http请求异常
     */
    public static HttpRequest produceHttpRequest(String path) throws HttpRequestException {
        // 判断请求是否为空
        if(StringVerify.isBlank(path)) throw new HttpRequestException("请求路径不能为空！");
        // 判断http请求与https请求
        if(Pattern.matches("http://.*",path)){
            return new HttpRequestImpl(path);
        }else if(Pattern.matches("https://.*",path)){
            return new HttpsRequestImpl(path);
        }else {
            throw new HttpRequestException("不支持该请求协议，请使用HTTP协议或HTTPS协议！");
        }
    }

}
